/*

An example of using the library for EA optimization, using a dummy evaluation function for testing purpose

Author	: Dudy Lim	
Email	: dudy0001@ntu.edu.sg

*/

#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<vector.h>
#include<iomanip.h>
#include "molCluster.h"
#include "population.h"


using std::string;

//
// A dummy evaluation function
//
vector<double> myEvaluator( vector<string>& stringArg, vector<double>& numericArg ) {

	vector<double> b(2);
	
	b[0] = mt(); b[1] = mt();

	return b;

}

int main( int arg, char** argv ) {

	srand(time(NULL));
	int i, j;
	int iteration = 20;
	int nElitist = 1;
	int mutationStrength = 2;

	string populationFile("population.dat");
	string molSettingFile("molSetting.dat");

	population parent( populationFile, molSettingFile, myEvaluator );
	population offspring( parent.size() );
		
	vector<string> stringArgs(2);
        vector<double> numericArgs(2);

	for(j=0; j<parent.size(); j++) 
		parent[j].evaluate( stringArgs, numericArgs );
	
	for(i=0; i<iteration; i++) {
			
		cout << "Iteration " << i+1 << ", best fitness is " << parent.bestFitness() << endl;
		
		for(j=0; j<offspring.size(); j++) {
			molCluster dad, mum; 
			parent.selectOneRandom( dad );	//note that other types of selections are also available (see population.h)
			parent.selectOneRandom( mum );	
			molCluster& child = offspring[j];
			
			dad.crossover( mum, child );
			child.mutate( mutationStrength );

			child.evaluate( stringArgs, numericArgs );
		}
		
		parent.replaceMuLambda( nElitist, offspring );
		
	}

	cout <<	"bestfitness " << parent.bestFitness() << endl;
	cout <<	"worstfitness " << parent.worstFitness() << endl;
	cout <<	"medianfitness " << parent.medianFitness() << endl;
	cout <<	"meanfitness " << parent.meanFitness() << endl;
	cout <<	"stdevfitness "  << parent.stdevFitness() << endl;


	return 0;

}
